package com.meter.data.protocol.result;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.meter.basic.domain.BasicMeter;
import com.meter.basic.service.impl.BasicMeterServiceImpl;
import com.meter.common.utils.DateUtils;
import com.meter.common.utils.HexUtils;
import com.meter.common.utils.StringUtils;
import com.meter.common.utils.spring.SpringUtils;
import com.meter.data.domain.MeterData;
import com.meter.data.service.impl.MeterDataServiceImpl;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Meter04 {

    /**
     * 保存恩勒曼热表数据
     * @param msgs
     */
    public static void saveMeter(String[] msgs, MeterData meterData){
        String meterNo = HexUtils.strChangeStrByLength(msgs, 7, 4, false);
        try {
            //热量
            String heatStr = msgs[26] + msgs[25] + msgs[24] + msgs[23];
            //热量单位
            String rldw = msgs[27];
            double heat = 0.0;
            if (rldw.equals("05")) {
                heat = Double.parseDouble(heatStr) * 0.01;
            } else if (rldw.equals("06")) {
                heat = Double.parseDouble(heatStr);
            }
            //流量
            double flow = Double.parseDouble(msgs[41] + msgs[40] + msgs[39] + "." + msgs[38]);
            //当前功率
            double power = Double.parseDouble(msgs[31] + msgs[30] + msgs[29] + "." + msgs[28]);
            double instant = Double.parseDouble(msgs[36] + msgs[35] + "." + msgs[34] + msgs[33]);
            //进水温度
            double inlet = Double.parseDouble(msgs[44] + "." + msgs[43]);
            //回水温度
            double outlet = Double.parseDouble(msgs[47] + "." + msgs[46]);
            //工作时间
            String workTime = msgs[51] + msgs[50] + msgs[49] + "";
            //运行时间
            String meterTime = "20" + msgs[57] + "-" + msgs[56] + "-" + msgs[55] + " " + msgs[54] + ":" + msgs[53] + ":" + msgs[52];

            meterData.setHeat(heat);
            meterData.setFlow(flow);
            meterData.setPower(power);
            meterData.setInstant(instant);
            meterData.setInlet(inlet);
            meterData.setOutlet(outlet);
            meterData.setWorkTime(workTime);
            meterData.setMeterTime(meterTime);
            SpringUtils.getBean(MeterDataServiceImpl.class).updateMeterData(meterData);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("表号：" + meterNo + " 数据解析错误，" + meterData.getReturnOrder());
        }
    }
}
